from django.db import models
from django.utils.module_loading import import_string

from av_manager.plat_form.plat_base import PlatFormBase
from common.file_utils import avatar_upload_to
from common.general_class import BaseModel

ACCOUNT_METHOD = 0
API_KEY = 1

PLAT_LOG_METHODS = [
    (ACCOUNT_METHOD, "帐号/密码"),
    (API_KEY, "API-KEY")
]
PLAT_CONTENT_VIDEO = 0
PLAT_CONTENT_TEXT = 1
PLAT_CONTENT = [
    (PLAT_CONTENT_VIDEO, "视频"),
    (PLAT_CONTENT_TEXT, "博文/说说")
]


class PlatForm(BaseModel):
    name = models.CharField(max_length=32, blank=True, null=True, verbose_name="平台名")
    code = models.CharField(max_length=15, blank=True, null=True, verbose_name="代号")
    host = models.URLField(verbose_name="平台地址")
    username = models.CharField(max_length=50, blank=True, null=True, verbose_name="平台帐号")
    password = models.CharField(max_length=80, blank=True, null=True, verbose_name="平台密码")
    login_method = models.SmallIntegerField(default=0, choices=PLAT_LOG_METHODS)

    class Meta:
        verbose_name = "平台表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"{self.id}-{self.name}"

    def get_manager(self) -> PlatFormBase:
        """获取平台管理类"""
        manager_position = f"{self._meta.app_label}.plat_form.{self.code.lower()}.manager.{self.code}"
        manager = import_string(manager_position)
        if self.login_method == API_KEY:
            return manager(self.password)
        return manager()


class ChannelSubscription(BaseModel):
    channel_id = models.CharField(max_length=32, blank=True, null=True, verbose_name="频道id")
    name = models.CharField(max_length=32, blank=True, null=True, verbose_name="频道名")
    avatar = models.ImageField(verbose_name='头像', upload_to=avatar_upload_to, default='', blank=True)
    description = models.TextField(blank=True, null=True, verbose_name="简介/描述")
    birthday = models.DateField(blank=True, null=True, verbose_name="诞生日期")
    v_count = models.IntegerField(blank=True, null=True, verbose_name="视频数量")
    s_count = models.IntegerField(blank=True, null=True, verbose_name="订阅者数量")
    plat_from = models.ForeignKey(PlatForm, db_constraint=False, on_delete=models.DO_NOTHING, verbose_name="平台")

    class Meta:
        verbose_name = "频道订阅表"
        verbose_name_plural = verbose_name


class CarriedContent(BaseModel):
    content_id = models.CharField(max_length=32, blank=True, null=True, verbose_name="内容id")
    source = models.CharField(max_length=15, blank=True, null=True, verbose_name="来源")
    content_type = models.SmallIntegerField(choices=PLAT_CONTENT, verbose_name="内容类型")
    name = models.CharField(max_length=500, blank=True, null=True, verbose_name="内容名")
    description = models.TextField(blank=True, null=True, verbose_name="简介/描述")
    publish_time = models.DateTimeField(blank=True, null=True, verbose_name="发布时间")
    material = models.ForeignKey("av_editor.MaterialFile", db_constraint=False, on_delete=models.DO_NOTHING, null=True,
                                 verbose_name="文件")
    channel = models.ForeignKey(ChannelSubscription, db_constraint=False, on_delete=models.DO_NOTHING, null=True,
                                verbose_name="频道")
    is_carried = models.BooleanField(default=False, verbose_name="是否已制作")

    class Meta:
        verbose_name = "视频搬运表"
        verbose_name_plural = verbose_name


